<?php

/**
 * CelebrioMail PeopleManagement
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    PeopleManagement
 */

namespace ApplicationsModule\PeopleModule;

use Nette\Object;
use Nette\String;
use Nette\Environment;

use \dibi;
use \DibiFluent;
use \DibiDataSource;
use \DibiRow;

use Celebrio\Core\ServiceProvider;
use Celebrio\Modules;

/**
 * People items, provided by the module and plugins
 *
 * @author pavel
 */
abstract class PeopleItems extends Object {

    const INCLUDE_PROTECTED = "protected";
    const EXCLUDE = "exclude";

    /**
     * Static list of the items provided by People application itself. The method returns
     * the actual items that represent database columns and also "virtual" items, that don't
     * appear in the database, but they figure in creating the people application forms
     * (namely the form for adding new contacts). The only parameter specifies the items
     * that will be excluded from the list so that if you don't need any of items, just pass
     * them in the array.
     *
     * @static
     * @param array $exclude
     * @return array Array of arrays - provided rows structure
     */
    static function providedRows(array $exclude = array("photo_person_hidden")) {
        $provided = array(
            array("db" => "id_user",            "display" => false,          "protected" => false, "unique" => true),
            array("db" => "first_name_person",  "display" => _('First name'),"protected" => false, "unique" => false),
            array("db" => "last_name_person",   "display" => _('Last name'), "protected" => false, "unique" => false),
            array("db" => "photo_person",       "display" => _('Photo'),     "protected" => false, "unique" => false, "type" => "file"),
            array("db" => "photo_person_hidden","display" => false,          "protected" => false, "unique" => false),
        );
        return array_filter($provided, function(array $item) use ($exclude) {
            return !in_array($item["db"], $exclude);
        });
    }

    /**
     * Returns all the columns that contact modules provide, including "base" columns
     * from the people application itself. Can be used for generating generic list
     * of contact items or querying database (people join all).
     *
     * @static
     * @param array $modifiers
     * @return array (string)|array
     */
    public static function info_items(array $modifiers = array()) {
        // replace default by argument if specified, ruby ftw
        $modifiers = array_merge(self::default_modifiers(), $modifiers);
        
        $contact_services = ServiceProvider::getAllServices(Modules::CONTACTS);
        $provided = array("first_name_person", "last_name_person", "photo_person", "id_user");

        foreach ($contact_services as $contact_service) {
            foreach($contact_service::providedRows() as $row) {
                if ($modifiers[self::INCLUDE_PROTECTED] || $row["protected"] === false) {
                    array_push($provided, $row["db"]);
                }
            }
        }

        foreach ($modifiers[self::EXCLUDE] as $exclude_item) {
            unset($provided[$exclude_item]);
        }

        return $provided;
    }

    private static function default_modifiers() {
        return array(
            self::INCLUDE_PROTECTED => false,
            self::EXCLUDE => array(),
        );
    }
    
}